package com.mall.inventory.server.service.impl;

import com.mall.common.core.domain.Result;
import com.mall.common.security.utils.SecurityUtils;
import com.mall.inventory.common.dto.CmsPurchaseOrderDto;
import com.mall.inventory.common.dto.CmsPurchasePlanDto;
import com.mall.inventory.common.po.CmsPurchaseOrder;
import com.mall.inventory.common.po.CmsPurchasePlan;
import com.mall.inventory.server.mapper.InventoryMapper;
import com.mall.inventory.server.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

import static com.alibaba.druid.util.JdbcSqlStatUtils.getData;
import static com.mall.common.core.utils.PageUtils.startPage;

@Service
public class InventoryServiceImpl implements InventoryService{

    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 商家端：
     * 添加采购计划
     * @param skuIds
     * @return
     */
    @Override
    public Result addCmsPurchasePlan(String skuIds) {
        if(skuIds==null || skuIds.equals("")){
            return Result.error("请选中数据后进行添加!");
        }
        String[] skuIdList = skuIds.split(",");
        //进行批量添加
        int i = inventoryMapper.addCmsPurchasePlan(skuIdList);
        if(i>0){
            return Result.success(null,"添加成功");
        }
        return Result.error("请稍后重试!");
    }

    /**
     * 商家端：
     * 添加采购订单
     * @param cmsPurchaseOrderDto
     * @return
     * @throws RuntimeException
     */
    @Override
    public Result addPurchaseOrder(CmsPurchaseOrderDto cmsPurchaseOrderDto) throws RuntimeException{
        //获取采购计划ids
        String planIds = cmsPurchaseOrderDto.getPlanIds();
        //将string转换成集合
        String[] planIdList = planIds.split(",");
        //查询采购计划
        List<CmsPurchasePlan> planList = inventoryMapper.findByPlanids(planIdList);
        //判断选择的采购计划状态是否为代采购
        for (CmsPurchasePlan cmsPurchasePlan : planList) {
            if(cmsPurchasePlan.getPlanStatus()==0){
                return Result.error("请先审核你选择的采购计划!");
            }
            if(cmsPurchasePlan.getPlanStatus()==2){
                return Result.error("请勿重复生成采购单!");
            }
        }
        //批量添加采购计划详情
        CmsPurchaseOrder cmsPurchaseOrder = new CmsPurchaseOrder();
        cmsPurchaseOrder.setSupplierId(cmsPurchaseOrderDto.getSupplierId());
//        cmsPurchaseOrder.setUserId(SecurityUtils.getUserId());
        cmsPurchaseOrder.setUserId(1L);
        //添加采购单
        int i = inventoryMapper.addPurchaseOrder(cmsPurchaseOrder);

        //添加采购单详情表
        int j = inventoryMapper.addPurchaseOrderDetail(cmsPurchaseOrder.getOrderId(), planIdList);


        if(i+j<2){
            //抛出异常
            throw new RuntimeException("添加失败");
        }
        return Result.success("生成采购单成功");
    }



    /**
     * 商家端：
     * 通过登录人信息查询所属店铺采购计划列表
     * @param cmsPurchasePlanDto
     * @return
     */
    @Override
    public Result CmsPurchasePlanList(CmsPurchasePlanDto cmsPurchasePlanDto) {
        startPage();
        //1.获取用户信息
        Long shopId = SecurityUtils.getLoginUser().getSysUser().getShopId();
        //2.通过店铺ID 查询采购计划列表
        List<CmsPurchasePlan> cmsPurchasePlanList = inventoryMapper.findByShopId(shopId);


        //3.返回用户优惠券列表
//        return getDataTable(cmsPurchasePlanList);
        return null;
    }
}
